package com.bawei.module_home.ui.news

import android.app.DatePickerDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_common.view.FlowLayoutWithAddView
import com.bawei.module_home.R
import com.bawei.module_home.adapter.NewsAdapter
import com.bawei.module_home.databinding.ActivityNewsAddBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.NewsBody
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@AndroidEntryPoint
@Route(path = Router.NEWS_ADD_PAGE)
class NewsAddActivity : BaseActivity<HomeServiceViewModel, ActivityNewsAddBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_news_add
    private val calendar = Calendar.getInstance()
    private val mViewModel: HomeServiceViewModel by viewModels()
    private val addresses = arrayOf("园区新闻", "月考新闻", "午间新闻", "千万千万千万千万")
    override fun initView() {
        super.initView()
        mBinding.toobar.apply {
            back.setOnClickListener({
                finish()
            })
            title.text="发布新闻"
            more.setImageResource(R.drawable.baseline_remove_red_eye_24)
        }
        updateDateInView()
        mBinding.i2.setOnClickListener({
            showDatePickerDialog()
        })
        // 使用 ArrayAdapter 设置数据
        val adapter: ArrayAdapter<String?> = ArrayAdapter<String?>(
            this,
            android.R.layout.simple_spinner_item,
            addresses
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.repairSelectAddress.setAdapter(adapter)

    }

    private fun showDatePickerDialog() {
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog(
            this,
            { _, selectedYear, selectedMonth, selectedDay ->
                // 用户选择日期后的回调
                calendar.set(selectedYear, selectedMonth, selectedDay)
                updateDateInView()
            },
            year,
            month,
            day
        )

        // 可选：设置日期范围限制
        // datePickerDialog.datePicker.minDate = ...
        // datePickerDialog.datePicker.maxDate = ...

        datePickerDialog.show()
    }

    private fun updateDateInView() {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        mBinding.e4.setText(dateFormat.format(calendar.time))
    }

    override fun initData() {

        mBinding.b1.setOnClickListener({
            val body= NewsBody(
                approved = true,
                published = true,
                content = mBinding.e5.text.toString(),
                id = 0,
                images = mBinding.flowImage.list,
                publishDate = mBinding.e4.text.toString(),
                publisher =mBinding.e3.text.toString(),
                title = mBinding.e1.text.toString(),
                type = mBinding.e2.text.toString()
            )
            if(body!=null){
                mViewModel.sendNews(body)
            }

        })
    }

    override fun initListener() {
        mViewModel._sendNewsState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed ->{}
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"发布成功")
                    finish()
                }
            }
        }
        mBinding.repairSelectAddress.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                val selectedAddress = (view as TextView).text.toString()
                mBinding.e2.setText(selectedAddress)
                mBinding.e2.setVisibility(View.GONE)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 未选择时的处理逻辑
            }
        })
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                FlowLayoutWithAddView.REQUEST_CAMERA -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
                FlowLayoutWithAddView.REQUEST_GALLERY -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
            }
        }
    }
}